package com.beyond.trace.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.domain.TraceBatch;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.stereotype.Service;
import com.beyond.trace.mapper.TraceConsumableMapper;
import com.beyond.trace.domain.TraceConsumable;
import com.beyond.trace.service.ITraceConsumableService;

/**
 * 消耗品信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceConsumableServiceImpl extends ServiceImpl<TraceConsumableMapper, TraceConsumable> implements ITraceConsumableService
{
    @Resource
    private TraceConsumableMapper traceConsumableMapper;

    /**
     * 查询消耗品信息
     * 
     * @param consumableId 消耗品信息ID
     * @return 消耗品信息
     */
    @Override
    public TraceConsumable selectTraceConsumableById(Long consumableId)
    {
        QueryWrapper<TraceConsumable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumable_id",consumableId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceConsumableMapper.selectOne(queryWrapper);
    }

    /**
     * 查询消耗品信息列表
     * 
     * @param traceConsumable 消耗品信息
     * @return 消耗品信息
     */
    @Override
    public List<TraceConsumable> selectTraceConsumableList(TraceConsumable traceConsumable)
    {
        QueryWrapper<TraceConsumable> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(traceConsumable.getConsumableType())) {
            queryWrapper.eq("consumable_type",traceConsumable.getConsumableType());
        }
        if(StringUtils.isNotEmpty(traceConsumable.getConsumableName())) {
            queryWrapper.like("consumable_name",traceConsumable.getConsumableName());
        }
        try{  ////解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return traceConsumableMapper.selectList(queryWrapper);
    }

    /**
     * 新增消耗品信息
     * 
     * @param traceConsumable 消耗品信息
     * @return 结果
     */
    @Override
    public boolean insertTraceConsumable(TraceConsumable traceConsumable)
    {
        if(StringUtils.isEmpty(traceConsumable.getConsumableNo())) {
            traceConsumable.setConsumableNo(SnowUtil.getId());
        }
        traceConsumable.setCreateTime(DateUtils.getNowDate());
        traceConsumable.setUpdateTime(DateUtils.getNowDate());
        traceConsumable.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceConsumable.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceConsumable.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return traceConsumableMapper.insertTraceConsumable(traceConsumable);
        return super.save(traceConsumable);
    }

    /**
     * 修改消耗品信息
     * 
     * @param traceConsumable 消耗品信息
     * @return 结果
     */
    @Override
    public int updateTraceConsumable(TraceConsumable traceConsumable)
    {
        traceConsumable.setUpdateTime(DateUtils.getNowDate());
        traceConsumable.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceConsumableMapper.updateById(traceConsumable);
    }

    /**
     * 批量删除消耗品信息
     * 
     * @param consumableIds 需要删除的消耗品信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceConsumableByIds(Long[] consumableIds)
    {
        QueryWrapper<TraceConsumable> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("consumable_id", Arrays.asList(consumableIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceConsumableMapper.delete(queryWrapper);
    }

    /**
     * 删除消耗品信息信息
     * 
     * @param consumableId 消耗品信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceConsumableById(Long consumableId)
    {
        QueryWrapper<TraceConsumable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumable_id", consumableId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceConsumableMapper.delete(queryWrapper);
    }
}
